package com.huluobo.h3kotlinproject

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.huluobo.h3kotlinproject.base.BaseMVVMActivity
import com.huluobo.h3kotlinproject.base.BaseViewModel
import com.huluobo.h3kotlinproject.cart.CartViewModel
import com.huluobo.h3kotlinproject.databinding.ActivityMainBinding


/**
 * 使用Navigation搭建主页面,步骤:
 * 1.在res下生成Navigation文件夹,创建Navigation图表
 * 2.添加几个fragment在图表中
 * 3.创建menu,有几个fragment写几个item,******注意****** item的id必须和图表中fragment的id一致,否则会找不到对应的fragment
 * 4.在main的xml中拉出一个控件 NavHostFragment ,直接拉会报错,需要修改为fragment
 * 5.给对应的navGraph 添加刚刚生成Navigation图表
 * defaultNavHost 属性是必须的,不要删, 在Navigation图表中startDestination 属性是控制首先展示哪个页面
 * 6.设置底部导航栏和导航控制器联动
 */
class MainActivity : BaseMVVMActivity<ActivityMainBinding>() {
    private lateinit var cartViewModel: CartViewModel
    override fun initData() {
        cartViewModel = ViewModelProvider(this)[CartViewModel::class.java]

        cartViewModel.getCartListSuccess.observe(this) {
            if (it.data.isNotEmpty()) {
                dataBinding.mainBottomNav.getOrCreateBadge(R.id.nav_main_cart).number = it.data.size
            }
        }
    }

    override fun onResume() {
        super.onResume()
        cartViewModel.getCartList()
    }

    override fun initView() {
        //获取导航控制器
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.main_nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController

        //绑定底部导航栏与导航控制器
        dataBinding.mainBottomNav.setupWithNavController(navController)

//        //设置导航角标提示
//        dataBinding.mainBottomNav.getOrCreateBadge(R.id.nav_main_cart).number = 12
    }

    override fun getLayoutId(): Int = R.layout.activity_main

}